/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.mobiubiq.directory.client.frame;

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.core.KuixConstants;
import org.kalmeo.util.frame.Frame;
import org.mobiubiq.directory.client.KuixClient;
import org.mobiubiq.directory.client.data.BookmarksDataProvider;
import org.mobiubiq.directory.client.data.PlatformCapabilitiesDataProvider;

/**
 *
 * @author Marnix Renne
 */
public class MainScreenFrame extends DataProviderBaseFrame {

    public MainScreenFrame() {
        super("main-screen.xml", new PlatformCapabilitiesDataProvider());
    }

    public boolean onMessage(Object identifier, Object[] arguments) {
        if (super.onMessage(identifier, arguments)) {
            if ("exit".equals((String) identifier)) {
                // get the midlet instance to invoke the Destroy() method
                KuixClient.getDefault().destroyImpl();
                //if the event has been processed, we return 'false' to avoid event forwarding to other frames
                return false;
            }
            if ("about".equals(identifier)) {
                // display a popup message
                Kuix.alert(Kuix.getMessage("CREDITS"), KuixConstants.ALERT_OK);
                return false;
            }
            if ("exitConfirm".equals(identifier)) {
                // display a popup message
                Kuix.alert(Kuix.getMessage("EXIT_CONFIRM"), KuixConstants.ALERT_YES | KuixConstants.ALERT_NO, "exit", null, null, null);
                return false;
            }
            if ("readNfc".equals(identifier)) {
                try {
                    // use reflection to obtain NFC Reader
                    Class frameClass = Class.forName("org.mobiubiq.directory.client.frame.NfcReader");
                    Frame frame = (Frame) frameClass.newInstance();

                    // push next frame
                    Kuix.getFrameHandler().pushFrame(frame);
                    return false;
                } catch (InstantiationException ex) {
                    Kuix.alert(ex);
                } catch (IllegalAccessException ex) {
                    Kuix.alert(ex);
                } catch (ClassNotFoundException ex) {
                    Kuix.alert(ex);
                }
            }
            if ("readBarcode".equals(identifier)) {
                try {
                    // push next frame
                    Kuix.getFrameHandler().pushFrame(new BarcodeReader());
                    return false;
                } catch (Throwable t) {
                    Kuix.alert(t);
                    return false;
                }
            }
            if ("readLocation".equals(identifier)) {
                // push next frame
                Kuix.getFrameHandler().pushFrame(new LocationReader());
                return false;
            }
            if ("openBookmarks".equals(identifier)) {
                // push next frame
                Kuix.getFrameHandler().pushFrame(new Bookmarks());
                return false;
            }
            if ("openSettings".equals(identifier)) {
                // push next frame
                Kuix.getFrameHandler().pushFrame(new Settings());
                return false;
            }
            if("deleteAllBookmarks".equals(identifier)) {
                try {
                    RecordStore.deleteRecordStore(BookmarksDataProvider.BOOKMARKS);
                } catch (RecordStoreException ex) {
                    Kuix.alert(ex);
                }
            }
            if("test".equals(identifier)) {
//                BarcodeObject object = new BarcodeObject();
//                object.setBarcode("http://mobiubiq.org/marnix");
//                Kuix.getFrameHandler().pushFrame(new Services(object));
                Kuix.getFrameHandler().pushFrame(new TestFrame());
            }
        }
        // return "true" makes the FramHandler to forward the message to the next frame in the stack
        return true;
    }
}
